package com.javachmapi.springbootemail.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Component
public class SendEmailImpl {

    @Autowired
    private MyEmailSenderImpl myEmailSender;

    public void sendMail(List<String> toUser, String subject, String text) throws MessagingException, UnsupportedEncodingException {
        //MimeMessage message = javaMailSender.createMimeMessage();

        MimeMessage message = myEmailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

        InternetAddress from = new InternetAddress();
        from.setAddress(myEmailSender.getUsername());
        from.setPersonal("宋利军", "UTF-8");
        helper.setFrom(from);
        helper.setTo(toUser.toArray(new String[toUser.size()]));
        helper.setSubject(subject);
        helper.setText(text, true);
        myEmailSender.send(message);
    }

    /**
     * 发送邮箱
     *
     * @return 单条数据
     */
    public void sendMail(String toUser, String subject, String text) throws MessagingException, UnsupportedEncodingException {
        //MimeMessage message = javaMailSender.createMimeMessage();

        MimeMessage message = myEmailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

        InternetAddress from = new InternetAddress();
        from.setAddress(myEmailSender.getUsername());
        from.setPersonal("宋利军", "UTF-8"); //设置发送人信息
        helper.setFrom(from);
        helper.setTo(toUser);
        helper.setSubject(subject);
        helper.setText(text, true);
        myEmailSender.send(message);

    }
}
